package ch.abcm.remsmobile.REMSMobile.powerSupply;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import ch.abcm.remsmobile.REMSMobile.R;
import ch.abcm.remsmobile.REMSMobile.externnotifier.ExternNotifierController;

public class PowerDisconnectReceiver extends BroadcastReceiver{
    
    private static final String TAG = "PowerDisconnectReceiver";
    private static final String ACTION_POWER_DISCONNECTED = "android.intent.action.ACTION_POWER_DISCONNECTED";
    private static final String ACTION_POWER_CONNECTED = "android.intent.action.ACTION_POWER_CONNECTED";
    
    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        Log.d(TAG, "IntentAction: "+ intentAction);
        
        if (intentAction.equalsIgnoreCase(ACTION_POWER_CONNECTED)) {
            Log.d(TAG, "POWER_CONNECTED");
            sendPowerExternNotification(context, context.getResources().getString(R.string.power_connected));
//            sendPowerExternNotification(context, context.getString(R.string.power_connected));
        }else if (intentAction.equalsIgnoreCase(ACTION_POWER_DISCONNECTED)) {
            Log.d(TAG, "POWER_DISCONNECTED");
            sendPowerExternNotification(context, context.getResources().getString(R.string.power_disconnected));
//            sendPowerExternNotification(context, context.getString(R.string.power_disconnected));
        }else {
            // If this happened, something went very wrong!
            Log.e(TAG, "Received a unknown Intent");
        }
    }

    private void sendPowerExternNotification(Context context, String message) {
        Intent sentPowerNotification = ExternNotifierController.getSimpleMessageIntent(message);
        context.sendBroadcast(sentPowerNotification);
    }

}
